<?php

namespace app\common\model;

use think\Model;

class User extends Model
{

    /** 通过特定条件获取用户信息
     * @param $where
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function userInfo($where){
        return self::where($where)->find();
    }

    /*** 账号信息修改
     * @param $where
     * @param $info
     * @return User
     */
    public static function userEdit($where,$info){
        return self::where($where)->update($info);
    }

    /** 通过特定条件获取用户信息
     * @param $where
     * @return array|Model|null
     */
    public static function userInfoRaw($where){
        return self::whereRaw($where)->find();
    }

    /** 查询用户某个字段值
     * @param $where
     * @param $field
     * @return string
     */
    public static function valInfo($where,$field = "uid"){
        $info = self::where($where)->value($field);
        return $info;
    }

    /*** 注册
     * @param $data
     * @return User|Model
     */
    public static function registered($data){
        return self::insertGetId($data);
    }

    /*** 修改密码
     * @param $user
     * @param $newPassword
     * @return User
     */
    public static function forgetPassword($user,$newPassword){
        return self::where('uid',$user['uid'])->update(['password'=>self::encPassword($newPassword,$user['encryption'])]);
    }

    /*** 修改支付密码
     * @param $user
     * @param $newPassword
     * @return User
     */
    public static function forgetPayPassword($user,$newPayPassword){
        return self::where('uid',$user['uid'])->update(['pay_password'=>self::encPassword($newPayPassword,$user['encryption'])]);
    }

    //密码加密
    public static function encPassword($password,$encryption){
        $enc = md5(md5($password).$encryption);
        return $enc;
    }


    public static function dataList($where,$page,$num,$field="*",$order="uid desc"){
        return self::where($where)
            ->field($field)
            ->page($page,$num)
            ->order($order)
            ->select();
    }

    public static function dataCount($where){
        return intval(self::where($where)->count());
    }
}